<?php

namespace humhub\modules\api\controllers;

use humhub\modules\post\models\Post;
use Yii;
use humhub\components\Controller;
use humhub\models\Setting;
use humhub\modules\user\models\User;

use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
#use yii\filters\auth\HttpBasicAuth;
#use yii\filters\auth\QueryParamAuth;

/**
 * 获取主面板Dashboard的Post内容列表 发现页的post推荐内容列表
 */
class DashboardController extends BaseGuestController // extends Controller
{
    public $modelClass = 'humhub\modules\post\models\Post';

    /**
     * @inheritdoc
     */
    public function behaviors(){
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                //QueryParamAuth::className(),
                HttpBearerAuth::className(),
            ],
        ];

        $behaviors['acl'] = [
            'class' => \humhub\components\behaviors\AccessControl::className(),
            'guestAllowedActions' => ['stream'],
        ];

        return $behaviors;
    }

    public function actions(){
        $actions = parent::actions();
        unset($actions['update'], $actions['create'], $actions['index'], $actions['delete']);
        return $actions;

        return [
            'stream' => [
                //'class' => \humhub\modules\dashboard\components\actions\DashboardStream::className()
                'class' => \humhub\modules\api\cccstream\DashboardStream::className(),
            ]
        ];
    }

    /**
     * 获取主面板Dashboard的Post内容列表
     *
     * GET api/dashboard/stream?limit=[4]&filters=&sort=c&from=[66]&mode=normal
     *
     * get.limit : 单次获取总量
     *
     * get.from : 起始wallEntryID，默认为空''=第一页
     *
     * get.p_type : 类型筛选: 空=全部, 0=图文, 1=任务, 2=课程, 3=视频
     *
     * @return array  output:返回的内容, counter:表示获取几个wallEntry,  entryIds:表示所有返回的wallEntryID, lastEntryId:列表中最后一个EntryId. （object中的p_type类型请参考get.p_type的说明,当p_type=1为任务时，则space里包含主任务信息，space里的users是部分成员信息, comment包含部分评论和评论总数，like包含赞总数和我可操作状态：=like表示可赞，=unlike表示可取消赞）; 新增:u_v=1/0是否+V
     * - toptype 说明
     * - 0: 内容流
     * - 1: URL（H5）
     * - 2: 直播入口
     */
    public function actionStream(){
        if( (!Yii::$app->user->id) || $this->guest_uid==Yii::$app->user->id ){
            $_GET['is_guest'] = 1;
        }else{
            $query = User::find();
            $query->leftJoin('user_follow', 'user.id=user_follow.object_id and object_model=:userClass and user_follow.user_id=:userId', [':userClass' => User::className(), ':userId' => Yii::$app->user->id]);
            $query->andWhere(['IS NOT', 'user_follow.id', new \yii\db\Expression('NULL')]);
            //用户未关注时，获取guest数据
            if( ! $query->count() ){
                $post=Post::find()->where(['created_by'=>Yii::$app->user->id])->one();
                if(!$post){
                    $_GET['is_guest'] = 1;
                }

            }
        }

        $_GET['is_ad'] = 1;
        $stream = Yii::createObject(\humhub\modules\api\cccstream\DashboardStream::className(), ['stream', $this]);
        Yii::$app->params['res-success'] = true;
        return $stream->run();
    }

    /**
     * 获取发现页的Post推荐内容列表
     *
     * GET api/dashboard/commend?limit=[4]&filters=&sort=c&from=[66]&mode=normal
     *
     * get.limit : 单次获取总量
     *
     * get.from : 起始wallEntryID，默认为空''=第一页
     *
     * get.p_type : 类型筛选: 空=全部, 0=图文, 1=任务, 2=课程, 3=视频
     *
     * @return array  output:返回的内容, counter:表示获取几个wallEntry,  entryIds:表示所有返回的wallEntryID, lastEntryId:列表中最后一个EntryId. （object中的p_type类型请参考get.p_type的说明,当p_type=1为任务时，则space里包含主任务信息，space里的users是部分成员信息, comment包含部分评论和评论总数，like包含赞总数和我可操作状态：=like表示可赞，=unlike表示可取消赞）; 新增:u_v=1/0是否+V
     * - toptype 说明
     * - 0: 内容流
     * - 1: URL（H5）
     * - 2: 直播入口
     */
    public function actionCommend(){

        $_GET['is_guest'] = 1;
        $_GET['is_ad'] = 0;

        $stream = Yii::createObject(\humhub\modules\api\cccstream\DashboardStream::className(), ['stream', $this]);
        Yii::$app->params['res-success'] = true;
        return $stream->run();
    }

}
